/*******************************************************************************
 * Copyright 2011 @ Kapil Viren Ahuja
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.kapil.framework.test.concurrent;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.testng.annotations.Test;

import com.kapil.framework.concurrent.IBroker;
import com.kapil.framework.concurrent.impl.AsyncQueueBroker;
import com.kapil.framework.test.core.BaseTestCase;

@SuppressWarnings("rawtypes")
public class ConcurrentTestCase extends BaseTestCase
{

    @Test
    public void testSimpleCase()
    {
        IBroker<Integer> broker = new AsyncQueueBroker<Integer>(5);
        ExecutorService threadPool = Executors.newFixedThreadPool(3);

        try
        {
            Future consumer1Status = threadPool.submit(new PrintIntegersConsumer("A", broker));
            Future consumer2Status = threadPool.submit(new PrintIntegersConsumer("B", broker));
            Future producerStatus = threadPool.submit(new IntegerProducers(broker));

            producerStatus.get();
            consumer1Status.get();
            consumer2Status.get();
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        catch (ExecutionException e)
        {
            e.printStackTrace();
        }
        finally
        {
            threadPool.shutdown();
        }
    }
}
